﻿namespace TfsBranchLib2.Model
{
    public class TfsItemExpandHelper
    {
        private int currentPieceIndex;
        private readonly int maxIndex;
        private string path;
        private readonly string[] pieces;
        private TfsItem tfsItem;

        public TfsItemExpandHelper(TfsItem tfsItem, string path)
        {
            this.path = path;
            this.tfsItem = tfsItem;
            pieces = path.Trim('/', '\\').Split('/', '\\');
            maxIndex = pieces.Length;
        }

        internal bool ExpandLevel()
        {
            if (currentPieceIndex < maxIndex)
            {
                if (!tfsItem.IsSubItems())
                {
                    tfsItem.GetItemForPath();
                    tfsItem.IsExpanded = true;
                }
                var subItem = tfsItem.FindSubItem(pieces[currentPieceIndex]);
                if (subItem != null)
                {
                    tfsItem = subItem;
                    currentPieceIndex++;
                    return false;
                }
                return true;
            }
            tfsItem.Checked = true;
            return true;
        }
    }
}